package cn.fudong.bos.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.fudong.bos.domain.base.Courier;
import cn.fudong.bos.domain.base.Standard;
import cn.fudong.bos.service.base.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

	// 模型驱动
	private Courier courier = new Courier();

	@Override
	public Courier getModel() {
		return courier;
	}

	// 注入Service对象
	@Autowired
	private CourierService courierServce;

	// 保存操作

	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String save() {

		courierServce.save(courier);
		return SUCCESS;
	}

	// 属性驱动

	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	// 加载页面:分页查询操作
	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })

	public String pageQuery() {

		Pageable pageable = new PageRequest(page - 1, rows);

		// 构造一个specification对象

		Specification<Courier> specification = new Specification<Courier>() {

			// 创建一个匿名内部类构造Specification
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> predicates = new ArrayList<Predicate>();

				// 工号
				if (StringUtils.isNotBlank(courier.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					predicates.add(p1);
				}
				// 公司
				if (StringUtils.isNotBlank(courier.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + courier.getCompany() + "%");
					predicates.add(p2);
				}
				// 快递员类型
				if (StringUtils.isNotBlank(courier.getType())) {
					Predicate p3 = cb.like(root.get("type").as(String.class), courier.getType());
					predicates.add(p3);
				}
				// 多表查询
				Join<Courier, Standard> standardJoin = root.join("standard", JoinType.INNER);
				if (courier.getStandard() != null && courier.getStandard().getId() != null) {
					Predicate p4 = cb.equal(standardJoin.get("id").as(Integer.class), courier.getStandard().getId());
					predicates.add(p4);

				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}
		};

		// 调业务层方法查询分页数据
		Page<Courier> pageData = courierServce.findPageData(specification, pageable);

		Map<String, Object> result = new HashMap();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());

		// 将map转换为json数据返回 ，使用struts2-json-plugin 插件
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;

	}

	// 属性驱动
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	// 批量作废快递员操作courier_delBatch
	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String delBatch() {
		// 接收ids,使用分割
		String[] idArray = ids.split(",");
		// 调用业务层，批量作废
		courierServce.delBatch(idArray);

		return SUCCESS;

	}

	// 批量还原操作
	@Action(value = "courier_RestoreBatch", results = {
			@Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String RestoreBatch() {
		// 接收ids,使用分割
		String[] idArray = ids.split(",");
		// 调用业务层，批量还原
		courierServce.RestoreBatch(idArray);
		return SUCCESS;
	}

	public String courier_pageQuery() {

		Pageable pageable = new PageRequest(page - 1, rows);

		// 构造一个specification对象

		Specification<Courier> specification = new Specification<Courier>() {

			// 创建一个匿名内部类构造Specification
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> predicates = new ArrayList<Predicate>();

				// 工号
				if (StringUtils.isNotBlank(courier.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					predicates.add(p1);
				}
				// 公司
				if (StringUtils.isNotBlank(courier.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + courier.getCompany() + "%");
					predicates.add(p2);
				}
				// 快递员类型
				if (StringUtils.isNotBlank(courier.getType())) {
					Predicate p3 = cb.like(root.get("type").as(String.class), courier.getType());
					predicates.add(p3);
				}

				// 多表查询

				Join<Object, Object> standardRoot = root.join("standard", JoinType.INNER);

				if (courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName())) {
					// 收派标准名字的模糊查询
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class),
							"%" + courier.getStandard().getName() + "%");
					predicates.add(p4);
				}

				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};

		// 调业务层方法查询分页数据
		Page<Courier> pageData = courierServce.findPageData(specification, pageable);

		Map<String, Object> result = new HashMap();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());

		// 将map转换为json数据返回 ，使用struts2-json-plugin 插件
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;

	}

	// 定区关联快递员,查新未关联定区的快递员 courier_findnoassociation
	@Action(value = "courier_findnoassociation", results = { @Result(name = "success", type = "json") })

	public String findnoassociation() {
			//调业务层
		List<Courier> Couriers = courierServce.findnoassociation();
			//压入值栈
		ActionContext.getContext().getValueStack().push(Couriers);
		
		return SUCCESS;

	}

}
